using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using Lexalytics;

namespace SalienceEngineDemo
{
    public partial class ctrlGetDocumentThemes : UserControl, DemoControl
    {
        public RichTextBox oTextBox;
        List<SalienceTheme> oGridResults;

        public ctrlGetDocumentThemes(RichTextBox oTextBox)
        {
            InitializeComponent();
            this.oTextBox = oTextBox;
            EnableProcessButton();
        }

        public void processText()
        {
            gridResults.Rows.Clear();
            try
            {
                SalienceDemo.oSalienceSession.PrepareText(this.oTextBox.Text);
                List<SalienceTheme> oResults = SalienceDemo.oSalienceSession.GetDocumentThemes();
                oGridResults = new List<SalienceTheme>();

                if (oResults.Count > 0)
                {
                    //Put the results in the grid
                    int nCount = 0;
                    foreach (SalienceTheme oPhrase in oResults)
                    {
                        gridResults.Rows.Add();
                        gridResults.Rows[nCount].Cells[0].Value = oPhrase.sTheme;
                        gridResults.Rows[nCount].Cells[1].Value = oPhrase.fScore;
                        gridResults.Rows[nCount].Cells[2].Value = oPhrase.nThemeType;
                        gridResults.Rows[nCount].Cells[3].Value = oPhrase.fSentiment;
                        gridResults.Rows[nCount].Cells[4].Value = oPhrase.nEvidence;
                        nCount++;
                        oGridResults.Add(oPhrase);
                    }
                    gridResults.AutoResizeRows();
                }
                else
                {
                    MessageBox.Show("No Themes Available", "GetDocumentThemes()");
                }
            }
            catch (SalienceException e)
            {
                MessageBox.Show(e.Message, "Salience Demo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btnProcess_Click(object sender, EventArgs e)
        {
            processText();
        }

        private void gridResults_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            string myContent = this.oTextBox.Text;
            Dialogs.ThemeDetails dlgThemeDetails = new SalienceEngineDemo.Dialogs.ThemeDetails(gridResults, oGridResults, myContent);
            dlgThemeDetails.ShowDialog();
        }

        private void getDocumentSentiment_VisibleChanged(object sender, EventArgs e)
        {
            gridResults.Rows.Clear();
        }

        #region DemoControl interface methods
        public void btnOptions_Click(object sender, EventArgs e)
        {
            ((SalienceDemo)this.Parent).StartOptionsDialog((int)Dialogs.Options.OptionSections.DOCUMENTS);
            EnableProcessButton();
        }

        public void SetFontSize()
        {
            this.gridResults.DefaultCellStyle.Font = new Font(this.oTextBox.Font.FontFamily, Properties.Settings.Default.fFontSize);
            this.gridResults.AutoResizeRows();
        }

        public void EnableProcessButton()
        {
            // Process button is enabled based on existence of content to process
            this.btnProcess.Enabled = (oTextBox.Text.Trim().Length > 0);
        }
        #endregion
    }
}
